﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.BLL.Core.IService;
using Empires.CRL.DataContract;
using Empires.RAL.Access;
using Empires.RAL.Pump;
using Empires.BLL.ServerProvider;

namespace Empires.BLL.Core.Implement
{
    public class CompanyNewsService : ICompanyNewsService
    {

        public List<DefaultDataPack> GetNewsList(string copName, string type, string subject, int page, int size)
        {
            ICompanyNewsAccess service = new CompanyNewsDataPump();
            int itemCount = GetNewsItemCount(copName, type, subject);
            return CompanyHelper.InjuectPagingInfo((currentPage) =>
            {
                return service.GetNewsList(copName, type, subject, currentPage, size);
            },
            itemCount, page, size);

            //int pagecount = itemCount % size == 0 ? itemCount / size : itemCount / size + 1;
            //page = page < 0 ? 0 : page;
            //page = page > pagecount - 1 ? pagecount - 1 : page;
            //int previouspage = page == 0 ? 0 : page - 1;
            //int nextpage = page == pagecount - 1 ? pagecount - 1 : page + 1;
            //var list = service.GetNewsList(copName, type, subject, page, size);

            //return list.Select(c =>
            //{
            //    c.ITEMCOUNT = itemCount;
            //    c.PAGESIZE = size;
            //    c.CURRENTPAGE = page + 1;
            //    c.PAGECOUNT = pagecount;
            //    c.PREVIOUSPAGE = previouspage;
            //    c.NEXTPAGE = nextpage;
            //    c.FIRSTPAGE = 0;
            //    c.LASTPAGE = pagecount - 1;
            //    return c;
            //}).ToList();
        }

        public int GetNewsItemCount(string copName, string type, string subject)
        {
            ICompanyNewsAccess service = new CompanyNewsDataPump();
            return service.GetNewsItemCount(copName, type, subject);
        }

        public List<DefaultDataPack> GetNewsDetail(int nid)
        {
            ICompanyNewsAccess service = new CompanyNewsDataPump();
            return service.GetNewsDetail(nid);
        }


        public int DeleteNewsItem(string copName, int id)
        {
            ICompanyNewsAccess service = new CompanyNewsDataPump();
            return service.DeleteNewsItem(copName,id);
        }


        public int AddNewsItem(string cpname, string author, string content, string from, string title)
        {
            ICompanyNewsAccess service = new CompanyNewsDataPump();
            return service.AddNewsItem(cpname, author, content, from, title);
        }
    }
}
